<template>
    <div class="all">
        <!-- <div style="text-align:center;font-size:28px;font-weight:bold;">{{$route.user_name}}</div> -->
        <!-- <div class="search">
            <el-form ref="form" :model="promoterSearch" :inline="true" size="mini">
                <el-form-item label="推广人：">
                    <el-input v-model="promoterSearch.user_name" clearable placeholder="请输入推广人"></el-input>
                </el-form-item>
                <el-form-item label="合作负责人：">
                    <el-input v-model="promoterSearch.principal" clearable placeholder="请输入合作负责人"></el-input>
                </el-form-item>
                <el-form-item label="负责人电话：">
                    <el-input
                        v-model="promoterSearch.principalPhone"
                        clearable
                        placeholder="请输入负责人电话"
                    ></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="searchClick">查 询</el-button>
                </el-form-item>
            </el-form>
        </div> -->
        <!-- <div class="btn">
            <el-button size="medium" type="primary" @click="add()">添 加</el-button>
        </div> -->
        <el-page-header @back="goBack" :content="$route.name" class="mb10"></el-page-header>
        <div class="table">
            <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
                <el-menu-item index="">
                    <el-badge :value="badgenum[0]" class="item" :max="99">
                        <span @click="searchClick(),pagestate=null">全部</span>
                    </el-badge>
                </el-menu-item>
                
                <el-menu-item index="0">
                    <el-badge :value="badgenum[1]" class="item" type="info" :max="99">
                        <span @click="withoutClick(),pagestate=0">仅注册</span>
                    </el-badge>
                </el-menu-item>

                <el-menu-item index="1">
                    <el-badge :value="badgenum[2]" class="item" type="primary" :max="99">
                        <span @click="haveClick(),pagestate=1">已添加</span>
                    </el-badge>
                </el-menu-item>
            </el-menu>
            <!-- 表格 -->
            <el-table
                :data="promoterTable"
                border
                height="560"
                :style="{width:screenWidth+'px'}"
                :cell-style="cellStyle"
                :header-cell-style="rowClass"
                @selection-change="allSelectionChange"
            >
                <!-- <el-table-column type="selection" width="60px"></el-table-column> -->
                <el-table-column label="序号" width="80px">
                    <template slot-scope="scope">
                        <span>{{ scope.row.id }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="用户名" width="160px">
                    <template slot-scope="scope">
                        <span @click="details(scope.row)" class="cup">{{ scope.row.user_name }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="手机号" width="160px">
                    <template slot-scope="scope">
                        <span>{{ scope.row.phone }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="车牌/车架" width="160px">
                    <template slot-scope="scope">
                        <span style="display:block;cursor:pointer;" @click="lookCar(scope.$index, scope.row)">查看</span>
                    </template>
                </el-table-column>
                <el-table-column label="加入时间" width="160px">
                    <template slot-scope="scope">
                        <span>{{ scope.row.create_time*1000 | formatDate }} {{ scope.row.create_time*1000 | formatDateBehind }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="洗车记录" width="160px">
                    <template slot-scope="scope">
                        <span style="display:block;cursor:pointer;" @click="lookCarWash(scope.$index, scope.row)">查看</span>
                    </template>
                </el-table-column>
                <el-table-column label="充值记录" width="160px">
                    <template slot-scope="scope">
                        <span style="display:block;cursor:pointer;" @click="lookTopup(scope.$index, scope.row)">查看</span>
                    </template>
                </el-table-column>
                <el-table-column label="消费总金额" width="120px">
                    <template slot-scope="scope">
                        <span>{{ scope.row.consumption }}元</span>
                    </template>
                </el-table-column>
                <el-table-column label="充值总金额" width="120px">
                    <template slot-scope="scope">
                        <span>{{ scope.row.recharge }}元</span>
                    </template>
                </el-table-column>
                <el-table-column label="积分(已使用)" width="120px">
                    <template slot-scope="scope">
                        <span>{{ scope.row.integral_used }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="积分(未使用)" width="120px">
                    <template slot-scope="scope">
                        <span>{{ scope.row.integral_unused }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="240px">
                    <template slot-scope="scope">
                        <el-button
                            size="small"
                            type="primary"
                            @click="edit(scope.$index, scope.row)"
                        >修改</el-button>
                        <el-button
                            size="small"
                            type="danger"
                            @click="remove(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination
                @size-change="pageCount"
                @current-change="currentPageCount"
                :current-page="currentPage"
                :page-sizes="[10, 50,100,150, 200]"
                :page-size="currentCount"
                layout="prev, pager, next, jumper, sizes,total"
                :total="pageResultSize"
            ></el-pagination>
        </div>
        <div>
            <!-- 添加对话框 -->
            <el-dialog title="添加" width="60%" :visible.sync="addVisible" center @close="closeAdd">
                <el-form :model="addpromoter" ref="addpromoter" :rules="rules">
                    <el-form-item label="推广人：" prop="user_name" :label-width="formLabelWidth">
                        <el-input clearable v-model="addpromoter.user_name" placeholder="请输入推广人"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号：" prop="phone" :label-width="formLabelWidth">
                        <el-input clearable v-model="addpromoter.phone" placeholder="请输入手机号"></el-input>
                    </el-form-item>
                    <el-form-item label="门店：" prop="shop_id" :label-width="formLabelWidth">
                        <el-select v-model="addpromoter.shop_id">
                            <el-option v-for="item in shopuser_name" :key="item.id" :label="item.user_name" :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer">
                    <el-button @click="cancelAdd">取 消</el-button>
                    <el-button type="primary" @click="confirmAdd">确 定</el-button>
                </div>
            </el-dialog>
            <!-- 修改对话框 -->
            <el-dialog title="修改" width="60%" :visible.sync="editVisible" center @close="closeEdit">
                <el-form :model="editpromoter" ref="editpromoter" :rules="rules">
                    <el-form-item label="用户名：" prop="promoter" :label-width="formLabelWidth">
                        <el-input clearable v-model="editpromoter.user_name" placeholder="请输入推广人"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号：" prop="phone" :label-width="formLabelWidth">
                        <el-input clearable v-model="editpromoter.phone" placeholder="请输入手机号"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer">
                    <el-button @click="cancelEdit">取 消</el-button>
                    <el-button type="primary" @click="confirmEdit">确 定</el-button>
                </div>
            </el-dialog>
            <el-dialog center :visible.sync="dialogVisible" width="500px">
                <div style="text-align:center;">
                    <img width="250px" height="auto" :src="dialogImageUrl" alt />
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import "../../../../static/scss/page.scss";
import utils from "../../../util/request";
import {formatDate } from '../../../util/time.js';
export default {
    data() {
        return {
            // 分页
            // 状态
            pagestate: null,
            currentPage: 1, //初始页面
            currentCount: 10, //当前页面的数据数量
            pageResultSize: 10, //表格数据总数
            screenWidth: window.innerWidth, //可视宽度
            addVisible: false, //添加对话框
            editVisible: false, //修改对话框
            dialogVisible: false, //放大图片的对话框显示
            dialogImageUrl: "", //放大图片的路径
            formLabelWidth: "100px", //表单label宽度
            promoterSearch: {
                //查询框表单
                user_name: "", //推广人
                phone: "" //手机号
            },
            addpromoter: {
                //添加对话框表单
                id: "",
                user_name: "", //推广人
                phone: "", //手机号
                create_time: "",//创建时间
                code_url: "",//推广码地址
                shop_id: ""//门店
            },
            editpromoter: {
                //修改对话框表单
                user_name: "", //推广人
                phone: "" //手机号
            },
            rules:{
                user_name:[
                    {required:true,message:"请输入添加的推广人",trigger:"blur"}
                ],
                phone:[
                    {required:true,message:"请输入添加推广人手机号",trigger:"blur"}
                ],
                shop_id:[
                    {required:true,message:"请选择对应的店铺",trigger:"blur"}
                ]
            },
            promoterTable: [
                // {
                //     id: 1,
                //     user_name: "吉林", //推广人
                //     phone: "J" //手机号
                // },
                // {
                //     id: 2,
                //     user_name: "深圳", //推广人
                //     phone: "S" //手机号
                // },
                // {
                //     id: 3,
                //     user_name: "北京", //推广人
                //     phone: "B" //手机号
                // }
            ],
            shopuser_name: [],
            row:'',
            activeIndex: '',
            badgenum:[0,0,0],
        };
    },
    filters:{
         // 时间
		formatDate(time){
			var date = new Date(time);
			return formatDate(date, 'yyyy-MM-dd');
		},
		formatDateBehind(time){
			var date = new Date(time);
			return formatDate(date, 'hh:mm:ss')
		}
    },
    methods: {
        goBack: function() {
            // 页头的返回
            this.$router.go(-1);
        },
        searchClick() {
            // 查询
            // console.log(null)
            let data = {page:this.currentPage,list_rows:this.currentCount,promoter_id:this.row.id}
            // for(var i in this.promoterSearch) this.promoterSearch[i] !== '' ? data[i] = this.promoterSearch[i] : '';
            utils.request("/user/list","post",JSON.stringify(data)).then(resp=>{
                console.log(resp)
                this.promoterTable = resp.data.data.data;
                this.pageResultSize = resp.data.data.total;
            })
        },
        // 仅注册
        withoutClick(){
            let data = {page:this.currentPage,list_rows:this.currentCount,promoter_id:this.row.id,add_car:"0"}
            utils.request("/user/list","post",JSON.stringify(data)).then(resp=>{
                // console.log(resp)
                this.promoterTable = resp.data.data.data;
                this.pageResultSize = resp.data.data.total;
            })
        },
        // 添加车辆
        haveClick(){
            let data = {page:this.currentPage,list_rows:this.currentCount,promoter_id:this.row.id,add_car:"1"}
            utils.request("/user/list","post",JSON.stringify(data)).then(resp=>{
                // console.log(resp)
                this.promoterTable = resp.data.data.data;
                this.pageResultSize = resp.data.data.total;
            })
        },
        add() {
            // 添加券码活动
            this.addVisible = true;
        },
        confirmAdd() {
            // 确认添加
            this.$refs.addpromoter.validate((valid)=>{
                if(valid){       
                    let data = {}
                    for(var i in this.addpromoter) this.addpromoter[i] !== '' ? data[i] = this.addpromoter[i] : '';

                    let timestamp = Date.parse(new Date());
                    data.create_time = String(timestamp/1000)
                    data.code_url = null
                    // console.log(data)
                    utils.request("/promoter/add","post",JSON.stringify(data)).then(resp=>{
                        console.log(resp)
                        data.id = resp.data.data;
                        this.promoterTable.length < this.currentCount ? this.promoterTable.push(data) : '';
                        this.addVisible = false;
                        this.addpromoter.user_name = ""; //推广人
                        this.addpromoter.phone = ""; //手机号
                        this.addpromoter.shop_id = ""; //手机号
                    })
                }else{this.$message.error("添加信息不能为空")}
            })
        },
        cancelAdd() {
            // 取消添加
            this.addVisible = false;
            this.addpromoter.promoter = ""; //推广人
            this.addpromoter.phone = ""; //手机号
        },
        closeAdd() {
            // 关闭添加对话框
            this.addVisible = false;
            this.addpromoter.promoter = ""; //推广人
            this.addpromoter.phone = ""; //手机号
        },
        download(index, row) {
            // 下载
        },
        edit(index, row) {
            // 修改的触发函数
            this.editVisible = true;
            this.editpromoter.id = row.id; //表ID
            this.editpromoter.user_name = row.user_name; //推广人
            this.editpromoter.phone = row.phone; //手机号
            this.editpromoter.index = index; //手机号
            console.log(this.editpromoter)
        },
        confirmEdit() {
            // 确认修改
            this.$refs.editpromoter.validate((valid)=>{
                if(valid){
                    let data = {}
                    for(var i in this.editpromoter) i !== "index" ? data[i] = this.editpromoter[i] : '';
                    console.log(data)
                    utils.request("/promoter/update","post",JSON.stringify(data)).then(resp=>{
                        console.log(resp)
                        let key = this.editpromoter.index;
                        for(var o in data) this.promoterTable[key][o] = data[o];
                        this.editVisible = false;
                        this.editpromoter.promoter = ""; //推广人
                        this.editpromoter.phone = ""; //手机号
                    })       
                }
            })
        },
        cancelEdit() {
            // 取消修改
            this.editVisible = false;
            this.editpromoter.promoter = ""; //推广人
            this.editpromoter.phone = ""; //手机号
        },
        closeEdit() {
            // 关闭修改对话框
            this.editVisible = false;
            this.editpromoter.promoter = ""; //推广人
            this.editpromoter.phone = ""; //手机号
        },
        remove(index, row) {
            // 删除 二级确认
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                utils.request("/promoter/delete","post",JSON.stringify({id:row.id})).then(resp=>{
                    console.log(resp)
                    this.promoterTable.splice(index,1) 
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }) 
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
            
        },
        //   表格居中
        cellStyle: function({ row, column, rowIndex, columnIndex }) {
            return "text-align:center;";
        },
        rowClass: function({ row, rowIndex }) {
            return "text-align:center;";
        },
        allSelectionChange: function(val) {
            // 获取选中的内容
            console.log(val);
        },
        // lookUser(index, row){
        //     // 跳转用户
        //     this.$router.push({ path: "/spreadUser", query: { row: row } });
        // },
        lookCar(index,row){
            // 查看车辆信息
            console.log(index,row)
            this.$router.push({ path: "/carOperation", query: { row: JSON.stringify(row) } });
        },
        lookCarWash(index,row){
            // 查看洗车记录
            console.log(index,row)
            this.$router.push({ path: "/carWash", query: { row: JSON.stringify(row) } });
        },
        lookTopup(index,row){
            // 查看充值记录
            console.log(index,row)
            this.$router.push({ path: "/topupOrder", query: { row: JSON.stringify(row) } });
        },
        lookintegral(row){
            this.$router.push({ path: "/integralDetails", query: { row: JSON.stringify(row)} });
        },
        browse(index, row) {
            console.log(index, row);
            this.dialogImageUrl = 'https://dzylab.dpyun.site'+row.code_url;
            this.dialogVisible = true;
        },
        // 每页数据的数量
        pageCount: function(val) {
            this.currentCount = val;
            if(this.pagestate === null){   
                this.searchClick()
            }else if(this.pagestate === 0){
                this.withoutClick()
            }else if(this.pagestate === 1){
                this.haveClick()
            }
        },
        // 点击的第几页
        currentPageCount: function(val) {
            this.currentPage = val;
            if(this.pagestate === null){   
                this.searchClick()
            }else if(this.pagestate === 0){
                this.withoutClick()
            }else if(this.pagestate === 1){
                this.haveClick()
            }
        },
        createimg(index,row){
            // console.log(row)
            utils.request("/qrcode/qrcode","post",JSON.stringify({id: row.id})).then(resp=>{
                // console.log(resp)
                this.promoterTable[index].code_url = resp.data.data
            })
        },
        // 店铺名
        requestshops(){
            utils.request("/shop/list","post",JSON.stringify({page:1,list_rows:999999999})).then(resp=>{
                console.log(resp)
                this.shopuser_name = resp.data.data.data.map(item=>{
                    return{
                        id: item["id"],
                        user_name: item["user_name"]
                    }
                })
            })
        },
        handleSelect(key, keyPath) {
            // console.log(key, keyPath);
        }
    },
    created() {
        this.row = JSON.parse(this.$route.query.row)
        // console.log(this.row)
        // this.searchClick()
        // this.withoutClick()
        // this.haveClick()
        utils.request("/user/list","post",JSON.stringify({page:1,list_rows:999999999,promoter_id:this.row.id})).then(resp=>{
            console.log(resp)
            // this.badgenum[1] = 
            resp.data.data.data.forEach(item=>{
                if(item.add_car){
                    this.badgenum[2]++
                }else{
                    this.badgenum[1]++
                }
            })
            this.badgenum[0] = this.badgenum[1] + this.badgenum[2]
            console.log(this.badgenum)
        })
        let data = {page:this.currentPage,list_rows:this.currentCount,promoter_id:this.row.id}
        utils.request("/user/list","post",JSON.stringify(data)).then(resp=>{
            console.log(resp)
            // this.badgenum[0] = resp.data.data.total;
            this.promoterTable = resp.data.data.data;
            this.pageResultSize = resp.data.data.total;
            console.log(this.badgenum)
        })
        // data.add_car = "0"
        // data.add_car = "1"
        // utils.request("/user/list","post",JSON.stringify(data)).then(resp=>{
        //     console.log(resp)
        //     this.badgenum[2] = resp.data.data.total;
        //     console.log(this.badgenum)
        // })
        this.requestshops()
    }
};
</script>
<style scoped>
.el-menu.el-menu--horizontal{
    border: 0 solid;
}
.el-menu--horizontal>.el-menu-item{
    height: 40px;
    line-height: 40px;
    border: 1px solid #dbd9d9;
    border-radius: 5px 5px 0 0;
}
.el-menu--horizontal>.el-menu-item.is-active{
    color: #ffffff;
    background-color: #409EFF;
    border: 0;
    border-radius: 5px 5px 0 0;
}
</style>
